//
//  UILabel+extend.m
//  QCash
//
//  Created by 张葱 on 2019/4/29.
//  Copyright © 2019年 Qworks. All rights reserved.
//

#import "UILabel+extend.h"

@implementation UILabel (extend)
- (void)alignTop {
    CGSize fontSize = [self.text sizeWithFont:self.font];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [self.text stringByAppendingString:@"\n "];
}

- (void)alignBottom {
    CGSize fontSize = [self.text sizeWithFont:self.font];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
+(UILabel*)createLabelWith:(UIFont*)font  title:(NSString*)title frame:(CGRect)frame textAligin:(NSTextAlignment)textAlignment color:(UIColor*)color{
    UILabel *lab = [[UILabel alloc]initWithFrame:frame];
    lab.textAlignment = textAlignment;
    lab.font = font;
    lab.textColor = color;
    lab.text = title.length>0?title:@"";
    return lab;
}


- (CGSize)widh:(CGFloat)width{
    
    // 给 显示的文本一个区域
    CGSize contentMaxSizes = CGSizeMake(width, MAXFLOAT);
    // NSFontAttributeName 字体的大
    NSDictionary *attributesDicts = @{NSFontAttributeName:self.font};
    
#warning 计算文本实际宽高的时候， 计算的字体大小要和label中设置的字体大小保持一致

    CGSize contentRealSizes = [self.text boundingRectWithSize:contentMaxSizes options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDicts context:nil].size;
    
    return contentRealSizes;

}


@end
